package com.jeejam.product.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.jeejam.framework.base.service.impl.BaseServiceImpl;
//import com.jeejam.member.pojo.Vote;
//import com.jeejam.member.service.VoteService;
import com.jeejam.product.dao.ProductArticleMapper;
import com.jeejam.product.pojo.ProductArticle;
import com.jeejam.product.service.ProductArticleService;

@Service
public class ProductArticleServiceImpl extends BaseServiceImpl implements ProductArticleService{
	
	private ProductArticleMapper productArticleMapper;
	
//	private VoteService voteService;

	@Resource
	public void setProductArticleMapper(ProductArticleMapper productArticleMapper) {
		this.productArticleMapper = productArticleMapper;
		this.baseMapper = productArticleMapper;
	}

	@Override
	public int countsByUserId(int userId) {
		return productArticleMapper.countsByUserId(userId);
	}

	@Override
	public int countsByType(String type) {
		int counts = 0;
		if (type.equals("new")) {
			counts = this.countsForNew();

		} else if (type.equals("hot")) {
			counts = this.countsForHot();

		} else if (type.equals("recommend")) {
			counts = this.countsForRecommend();

		}
		return counts;
	}

	@Override
	public int countsForNew() {
		return productArticleMapper.countsForNew();
	}

	@Override
	public int countsForHot() {
		return productArticleMapper.countsForHot();
	}

	@Override
	public int countsForRecommend() {
		return productArticleMapper.countsForRecommend();
	}

	@Override
	public List<ProductArticle> findForUserId(int userId, int page, int pageSize) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("begin", (page-1)*pageSize);
		params.put("pageSize", pageSize);
		params.put("userId", userId);
		return productArticleMapper.selectForUserId(params);
	}

	@Override
	public List<ProductArticle> findForNew(int page, int pageSize) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("begin", (page-1)*pageSize);
		params.put("pageSize", pageSize);
		return productArticleMapper.selectAllForNew(params);
	}

	@Override
	public List<ProductArticle> findForHot(int page, int pageSize) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("begin", (page-1)*pageSize);
		params.put("pageSize", pageSize);
		return productArticleMapper.selectAllForHot(params);
	}

	@Override
	public List<ProductArticle> findForRecommend(int page, int pageSize) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("begin", (page-1)*pageSize);
		params.put("pageSize", pageSize);
		return null;
	}

	@Override
	public List<ProductArticle> findByType(String type, int page, int pageSize) {
		List<ProductArticle> articles = null;
		if (type.equals("new")) {
			articles = this.findForNew(page, pageSize);

		} else if (type.equals("hot")) {
			articles = this.findForHot(page, pageSize);

		} else if (type.equals("recommend")) {
			articles = this.findForRecommend(page, pageSize);

		}
		return articles;
	}

	@Override
	public int getVotes(int id) {
		return productArticleMapper.selectVotes(id);
	}

	@Override
	public void updateViews(int id) {
		productArticleMapper.updateViews(id);
	}

	@Override
	public void updateVotes(int votes, int id) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("votes", votes);
		params.put("id", id);
		productArticleMapper.updateVotes(params);
	}

	@Override
	public void recommend(int id) {
		productArticleMapper.recommend(id);
	}

	@Override
	public void cancelRecommend(int id) {
		productArticleMapper.cancelRecommend(id);
	}

	@Override
	public void trash(int id) {
		productArticleMapper.trash(id);
	}

	@Override
	public void restore(int id) {
		productArticleMapper.restore(id);
	}

//	@Override
//	public List<ProductArticle> showWithVote(int userId, List<ProductArticle> productArticles) {
//		List<ProductArticle> productArticleList = new ArrayList<ProductArticle>();
//		for(ProductArticle productArticle : productArticles){
//			Map<String, Object> map = new HashMap<String, Object>();
//			map.put("article", productArticle);
//			if(userId != 0){
//				Vote vote = voteService.selectForRecommendByUserId(productArticle.getId(), userId);
//	        	if(vote != null){
//	        		map.put("vote", vote.getValue());
//	        	}
//			}
//			productArticleList.add(map);
//
//		}
//		return productArticleList;
//	}

}
